<template>
  <div>
    <van-nav-bar title="登录" class="nav">
      <template #left>
        <van-icon class="icon" name="cross" size="20" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="form.mobile"
        name="mobile"
        placeholder="请输入手机号"
        type="number"
        :rules="userFormRules.mobile"
      >
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field
        v-model="form.code"
        type="text"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
      >
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <van-count-down :time="time" v-if="isShow" @finish="isShow = false">
            <template #default="timeData">
              <span class="block">{{ timeData.seconds }}</span> s
            </template>
          </van-count-down>
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            type="default"
            native-type="button"
            @click="onSendSmsCode"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, getSmsCode } from '@/api/user'
export default {
  name: 'loginPage',
  data() {
    return {
      form: {
        mobile: '13800000001',
        code: '246810'
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      },
      isShow: false,
      time: 60 * 1000
    }
  },
  methods: {
    async onSendSmsCode() {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (error) {
        return console.log(error)
      }
      try {
        await getSmsCode(this.form.mobile)
        this.isShow = true
        this.$toast.success('发送成功')
      } catch (err) {
        // 发送失败，关闭倒计时
        this.isShow = false
        if (err.response.status === 429) {
          this.$toast.fail('发送太频繁')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    },
    async onSubmit() {
      try {
        const res = await login(this.form)
        this.$store.commit('setUser', res.data.data)
        this.$toast.success('登录成功')
        this.$router.push('/home')
      } catch (error) {
        console.log('登录失败', error)
        this.$toast.fail('登录失败，手机号或验证码错误')
      }
    }
  }
}
</script>

<style scoped lang="less">
body {
  background-color: #000;
}
.nav {
  background-color: #3296fa;
}
::v-deep(.van-nav-bar__title) {
  color: #fff;
}
.icon {
  color: #fff;
}
.send-sms-btn {
  background-color: #ededed;
  padding: 0 8px;
  color: #666;
}
</style>
